from odoo import fields


class MarketingRecordAndOperationLogFunc(object):
    """
    Models:满足所有日志方面的算法逻辑
    Params:
        -env:odoo env
    """

    def __init__(self, env):
        self.env = env

    """
    --------------------------------------------------------------------------
    满足所有优惠券日志相关逻辑
    """

    def create_coupon_log(self, type, customer_id, coupon_id):
        """
        Params:
        -type:类型【生效，过期】
        -customer_id:客户ID
        -coupon_id:优惠券ID
        -
        """
        # 查找当天的同一分类的生效和过期记录
        now_date = fields.datetime.now()

        record = self.env['rp.marketing.action.log'].search([('root_category', '=', type), ('create_date', '>=', now_date.replace(hour=0, minute=0, second=0)), ('create_date', '<=', now_date.replace(hour=23, minute=59, second=59))])
        if len(record.ids) == 0:
            data = {
                'root_category': type,
                'rp_customer_id': customer_id.id,
                'customer_coupon_ids': [(4, coupon_id.id)]
            }
            self.env['rp.marketing.action.log'].create([data])
        else:
            record[0].write({'customer_coupon_ids': [(4, coupon_id.id)]})
